package com.k.base.jdk.stream.listbean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author kivil
 */
public class UserTest {
    private static List<User> list ;

    //**************************** select list ***********************************************************

    /**
     * select user m1
     * @param c condition
     * @return list
     */
    private static List<User> selectUser(User c){
        return list.stream().filter(new Predicate<User>() {
            @Override
            public boolean test(User t) {
                return  t.getAge() < c.getAge();
            }
        }).collect(Collectors.toList());
    }

    /**
     * select user m2
     * @param c condition
     * @return list
     */
    private static List<User> selectUser2(User c){
        Predicate<User> filterPredicate = e ->  e.getAge()<c.getAge();
        return list.stream().filter(filterPredicate).collect(Collectors.toList());
    }

    /**
     * select user m2
     * @param c condition
     * @return list
     */
    private static List<User> selectUser3(User c){
        return list.stream().filter(u -> u.getAge()<c.getAge()).collect(Collectors.toList());
    }


    /**
     * select count m1
     * @param c condition
     * @return count
     */
    private static Long selectUserCount(User c){
        Long count;
        // m1
        count = list.stream().filter(new Predicate<User>() {
            @Override
            public boolean test(User t) {
                return  t.getAge() < c.getAge();
            }
        }).count();

        return count;
    }

    /**
     * select count m2
     * @param c condition
     * @return count
     */
    private static Long selectUserCount2(User c){
        Predicate<User> filterPredicate = e ->  e.getAge()<c.getAge();
        return list.stream().filter(filterPredicate).count();
    }

    /**
     * select count m3
     * @param c condition
     * @return count
     */
    private static Long selectUserCount3(User c){
        return list.stream().filter(student -> student.getAge()<c.getAge()).count();
    }

    /**
     * 遍历 list bean
     * @param c condition
     */
    private static void forEach(User c){
        // m1
        Predicate<User> filterPredicate = e ->  e.getAge()<c.getAge();
        Consumer<User> printConsumer = e -> System.out.println(e.getId() + ", "+e.getName());
        list.stream().filter(filterPredicate).forEach(printConsumer);
    }


    //**************************** select by page ***********************************************************

    private static List<User> selectUserByPage(User c,int pageNo,int pageSize){
        System.out.println("pageNo:"+pageNo+" pageSize:"+pageSize);
        long offset = pageNo < 1? 1L: (long)(pageNo - 1) * pageSize;
        long limit = (long)pageSize;
        return list.stream().filter(s -> s.getAge()<c.getAge())
                .skip(offset)
                .limit(limit)
                .collect(Collectors.toList());
    }


    //**************************** listToMap *****************************************************************

    private static Map<String,String> listToMap(){
        return list.stream().collect(Collectors.toMap(User::getId, User::getName));
    }
    private static Map<String,User> listToMap2(){
        return list.stream().collect(Collectors.toMap(User::getId, s-> s));
    }
    private static Map<String,User> listToMap3(){
        return list.stream().collect(Collectors.toMap(User::getId, s-> s,(k1,k2)->k1));
    }


    //**************************** List<Bean> to List<NewBean> ****************************************************

    private static List<NewUser> listBeanToListNewBean(){
        return list.stream()
                .map(itm -> {
                    NewUser newUser = new NewUser();
                    newUser.setId(itm.getId());
                    newUser.setName(itm.getName());
                    return newUser;
                })
                .collect(Collectors.toList());
    }
    private static List<NewUser> listBeanToListNewBean2(){
        return list.stream()
                .map(itm -> { return new NewUser(itm.getId(),itm.getName());})
                .collect(Collectors.toList());
    }

    //**************************** List<Bean> to List<String> ****************************************************

    private static List<String> listBeanToListString(){
        List<String> result = list.stream().map(s -> s.getId()).collect(Collectors.toList());
        result = list.stream()
                .filter(s -> s.getAge()<22)
                .map(User::getId)
                .collect(Collectors.toList());
        return result;
    }

    /**
     * 排序 - 根据 LocalDateTime 字段排序
     */
    private static List<User> listBeanToCompare(List<User> userList){
        List<User> reverseList = userList.stream().sorted((a,b)->{
            return a.getStartDateTime().isAfter(b.getEndDateTime())?1:-1;
        }).collect(Collectors.toList());
        return reverseList;
    }

    /**
     * 过滤 - 修改 - 整理 - 返回
     * @return
     */
    private static List<User> updateElement(){
        return list.stream()
                .filter(a->a.getAge()>20)
                .peek(b->b.setName("jack"))
                .map(c->{User u = new User();u.setId(c.getId());u.setName(c.getName());return u;})
                .collect(Collectors.toList());
    }


    /**
     * init test data
     */
    private static void initData(){
        list = new ArrayList<>();
        for(int i=0;i<50;i++){
            User u = new User(""+i,"tom"+i,i+1);
            list.add(u);
        }
    }
    /**
     * Main test
     * @param args arg
     */
    public static void main(String[] args) {
        initData();

//        User c = new User("","",25);
//        List<User> list = selectUser(c);
//        List<User> list2 = selectUser2(c);
//        List<User> list3 = selectUser3(c);
//        System.out.println("list:"+list);
//        System.out.println("list2:"+list2);
//        System.out.println("list3:"+list3);
//
//        Long count = selectUserCount(c);
//        Long count2 = selectUserCount2(c);
//        Long count3 = selectUserCount3(c);
//        System.out.println("count:"+count);
//        System.out.println("count2:"+count2);
//        System.out.println("count3:"+count3);
//
//        int pageNo=1,pageSize=10;
//        List listByPage = selectUserByPage(c,pageNo++,pageSize);
//        List listByPage2 = selectUserByPage(c,pageNo++,pageSize);
//        List listByPage3 = selectUserByPage(c,pageNo++,pageSize);
//        System.out.println("listByPage:"+listByPage);
//        System.out.println("listByPage2:"+listByPage2);
//        System.out.println("listByPage3:"+listByPage3);
//
//        Map listToMap = listToMap();
//        Map listToMap2 = listToMap2();
//        Map listToMap3 = listToMap3();
//        System.out.println("listToMap:"+listToMap + " size:"+listToMap.size());
//        System.out.println("listToMap2:"+listToMap2 + " size:"+listToMap2.size());
//        System.out.println("listToMap3:"+listToMap3+" size:"+listToMap3.size());
//
//        List<NewUser> listBeanToListNewBean = listBeanToListNewBean();
//        System.out.println("listBeanToListNewBean:"+listBeanToListNewBean);
//
//        List<String> listBeanToListString = listBeanToListString();
//        System.out.println("listBeanToListString:"+listBeanToListString);
        List<User> list = updateElement();
        for(User u:list){
            System.out.println(u);
        }
    }

}
